package com.kfm.jdbc.day0404.thread;

public class Ticket implements Runnable{

    // 定义100张票
    private int ticket = 100;

    Object lock = new Object();

    @Override
    public void run() {
        // 模拟窗口一直在卖票
        while (true) {
            // 同步代码块
            synchronized (lock){
                if (ticket > 0) {
                    // 模拟卖票时间
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 模拟卖票操作
                    System.out.println(Thread.currentThread().getName() + " 正在卖第 " + ticket-- + "张票");
                }
            }
        }
    }
}
